﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FXnaInfrastructure;

using DotSpatial.Topology;

using System.Drawing;

using System.Drawing.Drawing2D;

using Point = System.Drawing.Point;





namespace FXnaGis
{

    /// <summary>
    /// 尝试用图形表示网格
    /// </summary>
    [Serializable]
    public class FGridShape:FShape
    {

        public FGridShape(string id,FGrid grid)
            : base(id)
        {


            this._grid = grid;

         
 

        }




        FGrid _grid;

        public FGrid Grid
        {
            get { return _grid; }
            set {
                
                _grid = value;

             
            
            
            }
        }



        public override void Draw(FMapArgs args)
        {

            this.DrawGrid(args);

            //this.DrawAxis(args);

           




        }



        public override bool Intersects(DotSpatial.Topology.Envelope envelope)
        {
            if (this.Extent.Intersects(envelope))
            {
                return true;

            }
            else
            {
                return false;
            }
           
        }


        public override void DrawLable(FMapArgs args)
        {
            base.DrawLable(args);

            //这里我们需要绘制坐标轴,以及对应的标注

            this.DrawAxis(args);

           




        }


        public override DotSpatial.Data.Extent Extent
        {
            get
            {
                return new DotSpatial.Data.Extent(this._grid.XMin, this._grid.YMin, this._grid.XMax, this._grid.YMax);
            }
        }


        #region 内部函数


        protected void DrawGrid(FMapArgs args)
        {
            LineStyle ls = this.Parent.LineStyle;


            GraphicsPath gp = new GraphicsPath();


            //先尝试着画行

            //这里先不考虑旋转的因素


            for (int i = 0; i < this._grid.NX; i++)
            {

                Coordinate p1 = new Coordinate(this._grid.GetXPos(i), this._grid.YMin);

                Coordinate p2 = new Coordinate(this._grid.GetXPos(i), this._grid.YMax);

                gp.StartFigure();

                gp.AddLine(args.ProjToPixel(p1), args.ProjToPixel(p2));





            }



            //然后划列

            for (int j = 0; j < this._grid.NY; j++)
            {


                Coordinate p1 = new Coordinate(this._grid.XMin, this._grid.GetYPos(j));

                Coordinate p2 = new Coordinate(this._grid.XMax, this._grid.GetYPos(j));


                gp.StartFigure();

                gp.AddLine(args.ProjToPixel(p1), args.ProjToPixel(p2));





            }




            ls.DrawPath(args.Device, gp, 1);


            gp.Dispose();
 
        }


        /// <summary>
        /// 绘制坐标轴的函数
        /// </summary>
        /// <param name="args"></param>
        protected void DrawAxis(FMapArgs args)
        {

            


           // LineStyle style = this.Parent.LineStyle;

           // LableStyle lableStyle = this.Parent.LableStyle;





           // GraphicsPath gp = new GraphicsPath();


           
           // //转换为绘制两点的函数？，总共欧4个轴

           // Coordinate start=new Coordinate ();

           // Coordinate end=new Coordinate ();

           // Point ptStart = args.ProjToPixel(start);
           // Point ptEnd = args.ProjToPixel(end);


           // //我们根据像素值来进行绘制，其中两端点必须绘制，然后中间在划分，我们这里可以采取只显示整数刻度的办法，留待进一步研究吧



           //// double len = 0;

            





           // style.DrawPath(args.Device, gp, 1);


           // gp.Dispose();





 
        }

        #endregion





    }
}
